goog.provide('discoinferno.Application');

//goog.require('discoinferno.model.Question');
//goog.require('discoinferno.ui.QuestionPane');
goog.require('goog.dom');
goog.require('goog.Timer');


discoinferno.Application = function(questionContainerId, timer) {
  this.questionContainerId = questionContainerId;
  //this.numQuestions = 10; //TODO: replace with serverside definition
  this.tickInterval = 3000;
  this.playerScore = 0;
  this.currentRightAnswer;
  this.timer = timer;
  this.startTime;
};

goog.require('goog.net.XhrIo');
goog.require('goog.bind');
/**
  * Retrieve JSON data using XhrIo's static send() method.
  *
  * @returns 
  */
discoinferno.Application.prototype.startQuestion = function() {
  //var score = 5;
  //var url_for_q = '/getQuestion'
  //if(score > 0){
  //  url_for_q += '?score='+score
  //}
  goog.net.XhrIo.send('/getQuestion', 
    goog.bind(function(e) {
      var xhr = e.target;
      var obj = xhr.getResponseJson();
      jsonQuestion = new discoinferno.model.Question(obj[0].questionText, obj[1].urls, obj[2].answerId);
      this.init(jsonQuestion);
  }, this));
}


discoinferno.Application.prototype.init = function(question) {
  //var question = new discoinferno.model.Question('Who is blazertes?', ['http://scientopia.org/blogs/childsplay/files/2010/08/cookie-monster.jpg','http://scientopia.org/blogs/childsplay/files/2010/08/cookie-monster.jpg','http://scientopia.org/blogs/childsplay/files/2010/08/cookie-monster.jpg','http://scientopia.org/blogs/childsplay/files/2010/08/cookie-monster.jpg'], 2);
  this.currentRightAnswer = question.correctAnswerIndex;
  var questionPane = new discoinferno.ui.QuestionPane(question,timer, goog.bind(this.startQuestion, this));
  var questionContainer = goog.dom.getElement(this.questionContainerId);
  goog.dom.appendChild(questionContainer, questionPane.element);
  
  
  //var score = goog.dom.createDom('a', {'class':'score','style':'display:none;font-size: 40px; margin-top: 20px;text-decoration:none;color:white;', 'href':'/leaders'}, 'Score: ' + question.score);
  //goog.dom.appendChild(questionPane.element, score);

  timer.start();

  var now = new Date();
  this.startTime = now.getTime();
  //instantiate backend services
  //post initial score of 0, get question
  //store question into question object
};

/*
  //for(var i = 0; i < this.numQuestions; i++) {
  //instantiate timer
  var timer = new goog.Timer(this.tickInterval);
  var currentQuestion = null; //somehow pull question from backend
  var currentQuestionPane = new discoinferno.ui.QuestionPane(currentQuestion, this.timer);
  var questionContainer = goog.dom.getElement(this.questionContainerId);
  
  //replace and display question
  goog.dom.removeChildren(questionContainer);
  goog.dom.appendChild(questionContainer, currentQuestionPane.element);

  timer.start();
  //}
  //complete, post score
}
discoinferno.Application.prototype.displayQuestionPane = function(questionPane) {
  
};*/
